package com.example.culture.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;


public class CmdUtil {
    //ffmpeg -i 0.mp3  -i 00.mp3 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f mp3 000.mp3
    //D:\work\项目\山财大\ffmpeg
    private static Logger logger = LoggerFactory.getLogger(CmdUtil.class);

//    private static String ffmpegPath=PropertiesUtils.getFmProperties();

    public static void exec(List<String> cmd) {
        if (logger.isInfoEnabled()) {
            logger.info("exec command: ");
            StringBuilder sb = new StringBuilder();//StringBuilder是字符串生成器，上面这部分和cmd操作没关系
            for (String c : cmd) {
                sb.append(c).append(" ");
            }
            logger.info(sb.toString());
        }
        //cmd操作部分
        try {
            ProcessBuilder builder = new ProcessBuilder();//创建新线程
            builder.command(cmd);//执行FFmpeg命令
            builder.redirectErrorStream(true);
            Process proc = builder.start();
            BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line;
            while ((line = stdout.readLine()) != null) {
            }
            proc.waitFor();
            stdout.close();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private static String path= PropertiesUtils.getProperties();

    /**
     *
     * @param songAccompany 歌曲伴奏
     * @param record 歌曲用户录音
     * @return
     */

    public static String mergeMp3(String songAccompany,String record) {
        songAccompany=path+songAccompany;
        record=path+record;
        String selfSingingOrigin=System.currentTimeMillis()+".mp3";
        String selfSinging=path+selfSingingOrigin;
        String fmPath="C:\\manage\\ffmpeg\\bin\\ffmpeg -i ";
//        String fmPath="D:\\work\\项目\\浙师大心音行项目\\ffmpeg-20200802-b48397e-win64-shared\\ffmpeg-20200802-b48397e-win64-shared\\bin\\ffmpeg -i ";
        Runtime  run=null;
        try {
            String cmd=fmPath+songAccompany+" -i " +record+" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f mp3 "+selfSinging;
            logger.info("cmd"+cmd);
            run=Runtime.getRuntime();
            final Process proc=run.exec(cmd);
//            OutHandler errorGobbler = new OutHandler(proc.getErrorStream(), "Error");
//            OutHandler outputGobbler = new OutHandler(proc.getInputStream(), "Info");
//            errorGobbler.start();
//            outputGobbler.start();
            proc.getOutputStream().close();
            proc.getInputStream().close();
            proc.getErrorStream().close();
            proc.waitFor();
            logger.info("歌曲合成成功{}"+selfSinging);
            return selfSingingOrigin;
        } catch (IOException | InterruptedException e) {
            logger.info("歌曲合成失败{}"+selfSinging);
            e.printStackTrace();
            return null;
        }finally {
            run.freeMemory();
        }
    }
}
